package com.tan.steamship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.domain.CommentPo;
import com.tan.steamship.domain.RoutesPo;
import com.tan.steamship.domain.UserPo;
import com.tan.steamship.mapper.CommentMapper;
import com.tan.steamship.service.CommentService;
import com.tan.steamship.service.RoutesService;
import com.tan.steamship.service.UserService;
import com.tan.steamship.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 85726
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2025-03-18 23:17:11
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentPo>
    implements CommentService{

    @Autowired
    UserService userService;

    @Autowired
    RoutesService routesService;

    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<CommentVO> getCommentsByBlogId(Integer routeId) {

        LambdaQueryWrapper<CommentPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CommentPo::getRouteId, routeId);
        List<CommentPo> comments = commentMapper.selectList(queryWrapper);
        List<CommentVO> commentVOS = new LinkedList<>();
        comments.forEach(e->{
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(e,vo);
            commentVOS.add(vo);
        });
        commentVOS.forEach(e -> {
            RoutesPo blog = e.getRoutesPo();
            e.setRoutesPo(blog);
        });
        return commentVOS.stream().sorted(Comparator.comparing(CommentVO::getCreateTime).reversed()).collect(Collectors.toList());
    }

    @Override
    public CommentVO addComment(Map<String, Object> para) {
        String content = String.valueOf(para.get("content"));
        Long routeId = Long.parseLong(para.get("routeId").toString());
        Long userId = Long.parseLong(para.get("userId").toString());
        long parentId = Long.parseLong(para.get("parentId").toString());
        UserPo user = userService.getById(userId);
        CommentVO comment = new CommentVO();
        comment.setContent(content);
        comment.setRouteId(routeId);
        comment.setRoutesPo(routesService.getById(routeId));
        comment.setUserId(userId);
        comment.setParentCommentId(parentId);
        comment.setNickname(user.getName());
        comment.setEmail(user.getEmail());
//        comment.setAvatar(user.getAvatar());
        comment.setAdminComment(user.getType().equals("1"));
        if (parentId != -1) {
            comment.setParentComment(commentMapper.selectById(parentId));
        }
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);
        return comment;
    }

}




